package newtank.view.context;

import entity.Direction;
import entity.Position;
import lombok.Data;
import newtank.view.TankView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lijiahao
 * @since 2022/1/27
 */
@Data
public class TankViewContext implements ModelViewContext {

    private String tankId;

    private int tHeight = 40;

    private int tWidth = 40;

    private int gunLength = 30;

    private int parentScreenWidth;

    private int parentScreenHeight;

    /** 子弹数量 **/
    private int bulletNumber;

    /**
     * 位置
     */
    private Position position;

    /**
     * 速度
     */
    private int speed;

    /**
     * 方向
     */
    private Direction direction = Direction.RIGHT;

    /**
     * 枪位置
     */
    private Position gunPosition;

    /**
     * 存活状态
     */
    private Boolean alive;

    /**
     * 版本号
     */
    private int version;

    public TankViewContext() {}

    public TankViewContext(int width, int height) {
        this.position = Position.getRandomPosition(width, height);
        this.direction = Direction.randomDirection();
        this.speed = 4;
        this.parentScreenHeight = height;
        this.parentScreenWidth = width;
        this.alive = true;
        this.version = 0;
    }

    public static TankView createView(int width, int height) {
        TankViewContext tankProperty = new TankViewContext(width, height);
        return new TankView(tankProperty);
    }

    public static List<TankView> createTankView(List<TankViewContext> tankProperties, int width, int height) {
        List<TankView> tankViewList = new ArrayList<>();
        for (TankViewContext context: tankProperties) {
            tankViewList.add(new TankView(context));
        }
        return tankViewList;
    }


    @Override
    public boolean isAlive() {
        return alive;
    }

    @Override
    public void dieOut() {
        this.alive = false;
    }

    @Override
    public int getVersion() {
        return this.version;
    }
}
